<?php
namespace app\admin\controller;
use app\service\adminService;
use auth\Auth;
use think\facade\Db;
use think\facade\View;

class Admin extends Common{
    public function fetchdata() {
        $type = request()->post('type');
        switch($type) {
            case 1:
                $usertype = request()->post('usertype');
                if($usertype == 1){
                    //查询物业信息
                    $department = (new adminService())->propertyInfo();
                    $arr = [];
                    foreach ($department as $val) {
                        $arr[] = $val['name'];
                    }
                }else if($usertype == 2){
                    //查询小区信息
                    $department = (new adminService())->communityInfo();
                    $arr = [];
                    foreach ($department as $val) {
                        $arr[] = $val['community_name'];
                    }
                }else if($usertype == 3){
                    //查询小区信息
                    $department = (new adminService())->serviceInfo();
                    $arr = [];
                    foreach ($department as $val) {
                        $arr[] = $val['name'];
                    }
                }
                return array_values($arr);
            default:
                return json(['info' => '查询错误']);
        }
    }
    public function adminList(){
        //查询管理员数据
        $lists = (new adminService())->adminList();
        //dump($lists);
        //根据角色ID查询角色名称
        foreach($lists as $lis){
            $_groupTitle = (new Auth())->getGroups($lis['id']);
            //dump($_groupTitle);
            $groupTitle = $_groupTitle[0]['title'];
            $lis['group'] = $groupTitle;
            $list[] = $lis;
        }
        //exit();
        //dump($lists);exit();
        View::assign('list',$list);
        //查询管理员数据条数
        $number = (new adminService())->adminNumber();
        $number = count($number);
        View::assign('number',$number);
        return View::fetch('admin_list');
    }
    public function adminAdd(){
        //查询角色数据
        $group = (new adminService())->authGroup();
        View::assign('group',$group);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //验证表单信息
            $validate = $this->validate($data,[
                'name|名称' => 'require' ,
                'usertype|用户类型' => 'require',
                'department|所属部门' => 'require',
                'password|密码' => 'require|alphaNum',
                'group|角色' => 'require',
                'department|部门' => 'require'
            ]);
            if($validate !== true){
                $this->error($validate);
            }
            //判断账号名称是否重复
            $info = (new adminService())->adminName($data['name']);
            if($info){
                $this->error('账号已存在');
            }
            //将密码md5加密
            $data['password'] = md5($data['password']);
            $data['time'] = date('Y-m-d H:i:s',time());
            $juese['group_id'] = $data['group'];
            unset($data['group']);
            unset($data['repass']);
            //根据部门名称查询ID
            if($data['usertype'] == 1){
                $data['department'] = (new adminService())->propertyNameId($data['department']);
            }else if($data['usertype'] == 2){
                $data['department'] = (new adminService())->communityNameId($data['department']);
            }else if($data['usertype'] == 3){
                $data['department'] = (new adminService())->serviceNameId($data['department']);
            }
            // dump($data);exit();
            //将数据插入数据库
            $result = (new adminService())->adminAdd($data);
            //判断数据是否插入成功
            if($result){
                $juese['uid'] = $result;
                $admingroup = (new adminService())->adminGroup($juese);
                return "添加管理员成功";
                $this->success('添加管理员成功',url('adminList'));
            }else{
                $this->error('添加管理员失败',url('adminAdd'));
            }
        }else{
            return View::fetch('admin_add');
        }
    }
    
    public function adminAdd2(){
        //查询角色数据
        $group = (new adminService())->authGroup();
        View::assign('group',$group);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //验证表单信息
            $validate = $this->validate($data,[
                'name|名称' => 'require' ,
                'usertype|用户类型' => 'require',
                'department|所属部门' => 'require',
                'password|密码' => 'require|alphaNum',
                'group|角色' => 'require',
                'department|部门' => 'require'
            ]);
            if($validate !== true){
                $this->error($validate);
            }
            //判断账号名称是否重复
            $info = (new adminService())->adminName($data['name']);
            if($info){
                $this->error('账号已存在');
            }
            //将密码md5加密
            $data['password'] = md5($data['password']);
            $data['time'] = date('Y-m-d H:i:s',time());
            $juese['group_id'] = $data['group'];
            unset($data['group']);
            unset($data['repass']);
            //根据部门名称查询ID
            if($data['usertype'] == 1){
                $data['department'] = (new adminService())->propertyNameId($data['department']);
            }else if($data['usertype'] == 2){
                $data['department'] = (new adminService())->communityNameId($data['department']);
            }else if($data['usertype'] == 3){
                $data['department'] = (new adminService())->serviceNameId($data['department']);
            }
            // dump($data);exit();
            //将数据插入数据库
            $result = (new adminService())->adminAdd($data);
            //判断数据是否插入成功
            if($result){
                $juese['uid'] = $result;
                $admingroup = (new adminService())->adminGroup($juese);
                return "添加管理员成功";
                $this->success('添加管理员成功',url('adminList'));
            }else{
                $this->error('添加管理员失败',url('adminAdd2'));
            }
        }else{
            return View::fetch('admin_add2');
        }
    }

    public function adminEdit(){
        if(request()->isPost()){
            $data=request()->post();
            if($data["openid"]!="0"){
                //查询该微信用户是否存在
                $wxuserinfo=Db::table("zz_wxuser")->where("openid",$data["openid"])->find();
                if(empty($wxuserinfo)){
                    return "该微信用户不存在";
                }
            }
            $res=Db::table("admin")->where('id',$data["id"])->update(["openid"=>$data["openid"]]);
            if($res){
                return "修改成功";
            }else{
                return "修改失败";
            }
        }else{
            $id = request()->param('id');
            $info =Db::name('admin')->where('id',$id)->find();
            if(empty($info)){
                return "用户不存在";
            }
            View::assign('info',$info);
            return View::fetch('admin_edit');
        }

    }
    // public function adminEdit(){
    //     //查询角色数据
    //     $group = (new adminService())->authGroup();
    //     View::assign('group',$group);
    //     if(request()->isGet()){
    //         $id = request()->param('id');
    //         if(!isset($id) && empty($id)){
    //             $this->error('用户不存在');
    //         }
    //         //查询管理员数据
    //         $info = (new adminService())->adminIdList($id);
    //         View::assign('info',$info);
    //         //根据当前ID查询角色id
    //         $groupId = (new adminService())->adminIdGroupId($id);
    //         View::assign('groupId',$groupId);
    //         return View::fetch('admin_edit');
    //     }else{
    //         $request=request();
    //         //验证表单信息
    //         $validate=  $this->validate($request->param (),[
    //             'id'        =>  'require|number',
    //             'name|名称' => 'require|length:6,15',
    //             'group|角色' => 'require'
    //         ]);
    //         if($validate !== true){
    //                 $this->error($validate);
    //         }
    //         //获取表单信息
    //         $data = [];
    //         foreach (['name', 'group', 'password'] as $val) {
    //             if ($request->has($val) && $request->param($val)) {
    //                 $data[$val] = $val == 'password' ? md5($request->param($val)) : $request->param($val);
    //             }
    //         }
    //         $data['id'] = $request->param('id');
    //         $data['time'] = date('Y-m-d H:i:s',time());
    //         $juese['group_id'] = $data['group'];
    //         $juese['uid'] = $data['id'];
    //         unset($data['group']);
    //         //修改数据
    //         $result = (new adminService())->adminUpdate($data);
    //         if($result){
    //                 $admingroup = (new adminService())->adminGroupUpdate($juese['uid'],$juese);
    //                 $this->success('修改管理员成功',url('adminList'));
    //         }else{
    //                 $this->error('修改管理员失败');
    //         }
    //     }
    // }
    public function adminDel(){
		$request = request();
		$id = $request->param('id');
		$validate = $this->validate($request->param(),[
			'id'	=>	'require|number'
		]);
		if($validate !== true){
			$this->error($validate);
        }
        if($id == 1){
            $this->success('不可删除超级管理员',url('adminList'));
        }
        //判断是否存在该数据
        $info = (new adminService())->adminIdList($id);
        if(empty($info)){
            $this->error('该管理员不存在');
		}
        $result = Db::name('admin')->delete($id);
        if($result){
            $groupId = Db::name('auth_group_access')->where('uid',$id)->delete();
            $this->success('删除管理员成功',url('adminList'));
		}else{
            $this->success('删除管理员失败',url('adminList'));
		}
    }

}